<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 使用事务确保数据一致性
        DB::transaction(function () {
            $this->createAdminUser();
            $this->createTestUsers();
        });

        $this->displayUserInfo();
    }

    /**
     * 创建管理员用户
     */
    private function createAdminUser(): void
    {
        User::updateOrCreate(
            ['id' => 1],
            [
                'name' => 'Orange Tech 管理员',
                'email' => 'admin@example.com',
                'phone' => '13146148179',
                'birthday' => '1990-01-01',
                'bio' => 'Orange Tech 管理员，负责商城运营管理',
                'password' => Hash::make('password'),
                'email_verified_at' => now(),
                'is_active' => true,
            ]
        );
    }

    /**
     * 创建测试用户
     */
    private function createTestUsers(): void
    {
        $testUsers = [
            [
                'name' => '张三',
                'email' => 'zhangsan@example.com',
                'phone' => null,
                'birthday' => '1992-05-15',
                'bio' => 'Orange 产品爱好者，喜欢最新的科技产品',
                'password' => Hash::make('password'),
                'email_verified_at' => now(),
                'is_active' => true,
            ],
            [
                'name' => '李四',
                'email' => null,
                'phone' => '13800138000',
                'birthday' => '1988-12-20',
                'bio' => '科技发烧友，关注数码产品动态',
                'password' => Hash::make('password'),
                'email_verified_at' => now(),
                'is_active' => true,
            ],
            [
                'name' => '王五',
                'email' => 'wangwu@example.com',
                'phone' => '13900139000',
                'birthday' => '1995-08-10',
                'bio' => '设计师，追求简约美学',
                'password' => Hash::make('password'),
                'email_verified_at' => now(),
                'is_active' => true,
            ],
            [
                'name' => '赵六',
                'email' => null,
                'phone' => '13700137000',
                'birthday' => '1991-03-25',
                'bio' => '程序员，热爱开源技术',
                'password' => Hash::make('password'),
                'email_verified_at' => now(),
                'is_active' => true,
            ]
        ];

        foreach ($testUsers as $userData) {
            User::updateOrCreate(
                ['email' => $userData['email'], 'phone' => $userData['phone']],
                $userData
            );
        }
    }

    /**
     * 显示用户信息
     */
    private function displayUserInfo(): void
    {
        $this->command->info('✅ 创建了 1 个管理员和 4 个测试用户');
        $this->command->info('   管理员: admin@example.com / password');
        $this->command->info('   测试用户: zhangsan@example.com / password');
    }
}
